﻿using SmallTool.BaseFile;
using SmallTool.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmallTool.frm
{
    public partial class frm_setting : Form
    {
        public frm_setting()
        {
            InitializeComponent();
        }
        private DataBaseConnectModel model = null;
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void frm_setting_Load(object sender, EventArgs e)
        {
            dgv_connectList.DataSource = BaseCS.instance.GetAll();
        }

        private void dgv_connectList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 获取选中行信息
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            int id = Convert.ToInt32(dgv_connectList.Rows[e.RowIndex].Cells[0].Value);
            model = BaseCS.instance.GetModelIP(id);
            if (model != null)
            {
                lbl_ID.Text = id.ToString();
                cb_open.Checked = model.IsOpen;
                txt_ip.Text = model.IP;
                txt_loginName.Text = model.LoginName;
                txt_password.Text = model.PassWord;
                btn_Option.Text = "修改";
                btn_clear.Enabled = true;
                btn_del.Enabled = model.IsOpen ? false : true;
            }
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            model = null;
            btn_clear.Enabled = false;
            btn_del.Enabled = false;
            lbl_ID.Text = "0";
            cb_open.Checked = false;
            txt_ip.Text = string.Empty;
            txt_loginName.Text = string.Empty;
            txt_password.Text = string.Empty;
            btn_Option.Text = "添加";
        }

        private void btn_Option_Click(object sender, EventArgs e)
        {
            string _ip = txt_ip.Text;
            string _loginName = txt_loginName.Text;
            string _pwd = txt_password.Text;
            if (string.IsNullOrWhiteSpace(_ip) || string.IsNullOrWhiteSpace(_loginName) || string.IsNullOrWhiteSpace(_pwd))
            {
                MessageBox.Show("请输入完整的信息");
                return;
            }

            // 判断是否有选择启用的地址
            int openId = BaseCS.instance.GetOpenID();
            if ((openId == 0 && !cb_open.Checked) || (openId > 0 && openId.ToString() == lbl_ID.Text.Trim() && !cb_open.Checked))
            {
                MessageBox.Show("请选择一个启用的地址");
                return;
            }
            else if (openId > 0 && openId.ToString() != lbl_ID.Text.Trim() && cb_open.Checked)
            {
                if (MessageBox.Show("是否启用新的连接地址?", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                else
                {
                    // 修改已经启用的连接地址为不启用
                    BaseCS.instance.UpdateOpenState();
                }
            }

            string txt = btn_Option.Text;
            if (txt.Trim().Equals("添加"))
            {
                model = new DataBaseConnectModel();
                model.ID = BaseCS.instance.GetMaxID() + 1;
                model.IP = txt_ip.Text.Trim();
                model.IsOpen = cb_open.Checked;
                model.LoginName = txt_loginName.Text.Trim();
                model.PassWord = txt_password.Text.Trim();
                BaseCS.instance.Save(model);
                dgv_connectList.DataSource = null;
                dgv_connectList.DataSource = BaseCS.instance.GetAll();
                btn_clear_Click(sender, e);
            }
            else if (txt.Trim().Equals("修改"))
            {
                if (model != null)
                {
                    model.IP = txt_ip.Text.Trim();
                    model.IsOpen = cb_open.Checked;
                    model.LoginName = txt_loginName.Text.Trim();
                    model.PassWord = txt_password.Text.Trim();
                    BaseCS.instance.Update(model);
                    dgv_connectList.DataSource = null;
                    dgv_connectList.DataSource = BaseCS.instance.GetAll();
                    btn_clear_Click(sender, e);
                }
            }
            BaseCS.instance.Init();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(lbl_ID.Text.Trim());
            if (id == 0)
            {
                MessageBox.Show("请选择要删除的连接地址");
                return;
            }
            DataBaseConnectModel model = BaseCS.instance.GetModelIP(id);
            if (model != null)
            {
                if (model.IsOpen)
                {
                    MessageBox.Show("不可删除已经启用的连接地址,请更换启用地址后再进行删除.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (MessageBox.Show("确定要删除当前的连接地址吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Yes)
                {
                    BaseCS.instance.Delete(model);
                    dgv_connectList.DataSource = null;
                    dgv_connectList.DataSource = BaseCS.instance.GetAll();
                    btn_clear_Click(sender, e);
                }
            }
        }
    }
}
